define(['butterfly/view', 'butterfly/notification'], function(View, Notification) {
  return View.extend({
    events: {
      'click #login': 'login'
    },

    onShow: function() {
      localStorage.removeItem("username");
      this.$('#username').val('');
      this.$("#password").val('');
    },

    login: function() {
      var username = this.$('#username').val();
      var password = this.$("#password").val();
      var is_cordova = (typeof cordova != "undefined")
      var url = is_cordova ? "http://115.28.1.119:18860" : "http://localhost:3000";
      $.ajax({
        url: url + '/system/api/system/mobile/accounts/login',
        method: 'POST',
        data: {
          username: username,
          password: password,
          appKey: '9ac10bdf29e6cf120294703c95a60878',
          appId: 'com.foreveross.bsl2'
        }
      }).success(function(data) {
        if(data.result) {
          localStorage.setItem("username", username);
          new Notification({
            message: '登录成功',
            type: 'info'
          }).show();
          window.butterfly.navigate('#main/index.html');
        } else {
          new Notification({
            message: '帐号或密码错误',
            type: 'error'
          }).show();
          this.$("#password").val("");
        }
      }).error(function() {
        new Notification({
          message: '帐号或密码错误',
          type: 'error'
        }).show();
        this.$("#password").val("");
      });
    }

  });
})
